using System;

namespace Kaleida.ServiceMonitor.Model
{
    public class VersionCheckResult
    {
        private Exception exception;
        private string latestVersionUrl;
        private Version latestVersionNumber;

        private VersionCheckResult()
        {
        }

        public static VersionCheckResult FoundLatestVersion(Version versionNumber, string url)
        {
            return new VersionCheckResult {latestVersionNumber = versionNumber, latestVersionUrl = url};
        }

        public static VersionCheckResult Error(Exception exception)
        {
            return new VersionCheckResult { exception = exception };
        }

        public bool Success
        {
            get { return latestVersionUrl != null; }
        }

        public bool ThrewError
        {
            get { return exception != null; }
        }


        public Exception Exception
        {
            get
            {
                if(exception == null)
                    throw new InvalidOperationException("An exception was not thrown whilst retrieving version details");

                return exception;
            }
        }

        public string LatestVersionUrl
        {
            get
            {
                if (latestVersionUrl == null)
                    throw new InvalidOperationException("Could not determine latest version");

                return latestVersionUrl;
            }
        }

        public Version LatestVersionNumber
        {
            get
            {
                if (latestVersionUrl == null)
                    throw new InvalidOperationException("Could not determine latest version");

                return latestVersionNumber;
            }
        }
    }
}